﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using Top.Api;
using Top.Api.Request;
using Top.Api.Response;

namespace AliSMS
{
    public partial class Form1 : Form
    {
        private AlibabaAliqinFcSmsNumSendRequest topRequest;
        String appKey = string.Empty;//app_key
        String appSecret = string.Empty;//App_Secret
        public Form1()
        {
            InitializeComponent();
            this.topRequest = new AlibabaAliqinFcSmsNumSendRequest();
        }

        public static bool IsTel(string tel)
        {
            bool result;
            try
            {
                if (string.IsNullOrEmpty(tel))
                {
                    result = false;
                }
                else
                {
                    result = Regex.IsMatch(tel, "^[1]+[3,4,5,6,7,8]+\\d{9}");
                }
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }
        private void SendSms()
        {
            try
            {
                ITopClient topClient = new DefaultTopClient("http://gw.api.taobao.com/router/rest", appKey, appSecret);
                AlibabaAliqinFcSmsNumSendResponse alibabaAliqinFcSmsNumSendResponse = topClient.Execute<AlibabaAliqinFcSmsNumSendResponse>(this.topRequest);
                MessageBox.Show(alibabaAliqinFcSmsNumSendResponse.Result.Success ? "成功" : "失败");
            }
            catch (Exception)
            {
                MessageBox.Show("异常了！");
            }
        }
        private void btSend_Click(object sender, EventArgs e)
        {
            appKey = tb_App_Key.Text.Trim();//app_key
            appSecret = tb_App_Secret.Text.Trim();//App_Secret
            String smstype = tb_sms_type.Text.Trim();//短信类型
            String smsfreesignname = tb_sms_free_sign_name.Text.Trim();//短信签名
            String recnum = tb_rec_num.Text.Trim();//手机号码
            String smstemplatecode = tb_sms_template_code.Text.Trim();//短信模板
            String smsparam = tb_sms_param.Text.Trim();//短信参数


            if (string.IsNullOrEmpty(appKey))
            {
                MessageBox.Show("AppKey不能为空!");
            }
            else
            {
                if (string.IsNullOrEmpty(appSecret))
                {
                    MessageBox.Show("AppSecret不能为空!");
                }
                else
                {
                    if (string.IsNullOrEmpty(smstype))
                    {
                        MessageBox.Show("短信类型不能为空!");
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(smsfreesignname))
                        {
                            MessageBox.Show("短信签名不能为空!");
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(recnum))
                            {
                                MessageBox.Show("短信号码不能为空!");
                            }
                            else
                            {
                                if (!Form1.IsTel(recnum))
                                {
                                    MessageBox.Show("短信号码不合法!");
                                }
                                else
                                {
                                    if (string.IsNullOrEmpty(smstemplatecode))
                                    {
                                        MessageBox.Show("短信模板不能为空!");
                                    }
                                    else
                                    {
                                        if (string.IsNullOrEmpty(smsparam))
                                        {
                                            MessageBox.Show("短信参数不能为空!");
                                        }
                                        else
                                        {
                                           
                                            topRequest.SmsType = smstype;
                                            topRequest.SmsFreeSignName = smsfreesignname;
                                            topRequest.SmsParam = smsparam;
                                            topRequest.RecNum = recnum;
                                            topRequest.SmsTemplateCode = smstemplatecode;
                                            new Thread(new ThreadStart(this.SendSms))
                                            {
                                                IsBackground = true
                                            }.Start();
                                        }

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

    }
}
